class Solution {
    // 替换所有的问号
    public String modifyString1(String s) {
        // 暴力解法：模拟修改
        int n = s.length();
        if (n == 1) { // 排除特殊情况
            char ch = s.charAt(0);
            return ch == '?' ? "a" : s;
        }
        StringBuilder sb = new StringBuilder();
        char[] ans = s.toCharArray();
        for (int i = 0; i < n; i++) {
            char ch = s.charAt(i);
            if (ch == '?') {
                if (i == 0) { // 没有前缀字符
                    // 拿到后缀字符
                    char r = s.charAt(1);
                    for (char j = 'a'; j <= 'z'; j++) {
                        if (j != r) {
                            sb.append(j);
                            ans[i] = j;
                            break;
                        }
                    }
                } else if (i == n-1) { // 没有后缀字符
                    // 拿到前缀字符（修改后的）
                    char l = ans[n-2];
                    for (char j = 'a'; j <= 'z'; j++) {
                        if (j != l) {
                            sb.append(j);
                            ans[i] = j;
                            break;
                        }
                    }
                } else {
                    // 拿到前缀和后缀
                    char l = ans[i-1]; // 修改后的
                    char r = s.charAt(i+1);
                    for (char j = 'a'; j <= 'z'; j++) {
                        if (j != l && j != r) {
                            sb.append(j);
                            ans[i] = j;
                            break;
                        }
                    }
                }
            } else {
                sb.append(ch);
                ans[i] = ch;
            }
        }
        return sb.toString();
    }


    public String modifyString(String s) {
        char[] ans = s.toCharArray();
        int n = ans.length;
        for (int i = 0; i < n; i++) {
            char ch = ans[i];
            if (ch == '?') { // 需要修改
                for (char j = 'a'; j <= 'z'; j++) { // 找到可以替换的
                    // 前面的字符 和 后面的字符都满足即可
                    if ((i == 0 || j != ans[i-1]) && (i == n-1 || j != ans[i+1])) {
                        ans[i] = j;
                        break;
                    }
                }
            }
        }
        return new String(ans);
    }

    // 提莫攻击
    public int findPoisonedDuration(int[] timeSeries, int duration) {
        int sum = 0;
        int n = timeSeries.length;
        for (int i = 0; i < n; i++) {
            // 判断中毒的时间是否是默认时长
            if (i + 1 < n && timeSeries[i + 1] < timeSeries[i] + duration) {
                sum += (timeSeries[i + 1] - timeSeries[i]);
            } else {
                sum += duration;
            }
        }
        return sum;
    }
}




